package com.ansatsing.templateMethod.withHook;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//泡咖啡：1把水煮沸--》2用沸水冲泡咖啡--》3把咖啡倒进杯子---》4加糖和牛奶
public class Coffee extends RecipeTemplate {

	@Override
	void brew() {//2用沸水冲泡咖啡
		System.out.println("Dripping Coffee through filter");
	}

	@Override
	void addCondiments() {//4加糖和牛奶
		System.out.println("Adding Sugar and Milk");
	}

	@Override
	boolean customerWantsCondiments() {//客户自己决定是否加调料--钩子方法重写
		String answer = getUserInput();

		if (answer.toLowerCase().startsWith("y")) {
			return true;
		} else {
			return false;
		}
	}
	private String getUserInput() {//接受键盘输入
		String answer = null;

		System.out.print("Would you like milk and sugar with your coffee (y/n)? ");

		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		try {
			answer = in.readLine();
		} catch (IOException ioe) {
			System.err.println("IO error trying to read your answer");
		}
		if (answer == null) {
			return "no";
		}
		return answer;
	}	
}
